package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

@Aspect
@Component
@Slf4j
/**
 * 切面类
 */
public class AutoFillAspect {
    /**
     * 定义切入点
     *  切入点的的位置
     *  Spring扫描带有@annotation标记的方法
     */
    @Pointcut("@annotation(com.sky.annotation.AutoFill) && execution(* com.sky.mapper.*.*(..))")
    public void pointcut() {
    }

    /**
     * 前置通知
     *     时间和用户id信息
     * @param joinPoint
     * @return
     */
    @Before("pointcut()")
    public void before(JoinPoint joinPoint) {
        log.info("aop开始操作");
        //获取方法
        MethodSignature method = (MethodSignature) joinPoint.getSignature();
        //获取该方法的注解
        AutoFill annotation = method.getMethod().getAnnotation(AutoFill.class);
        //获取该方法的注解的类型
        OperationType valueType = annotation.value();

        //获取参数
        Object[] args = joinPoint.getArgs();
        //判断参数是否为空
        if (args == null || args.length == 0) {
            //为空  返回退出
            return;
        }
        //不为空，获取第一个参数的实体 对象
        Object entity = args[0];

        //准备数据  当前时间    用户id
        LocalDateTime now = LocalDateTime.now();
        Long currentId = BaseContext.getCurrentId();

        //根据注解类型 进行赋值
        if (valueType == OperationType.INSERT) {
            //是添加  就有四个公共字段数据
            try {
                Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                setCreateTime.invoke(entity, now);
                setUpdateTime.invoke(entity, now);
                setCreateUser.invoke(entity, currentId);
                setUpdateUser.invoke(entity, currentId);

            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        } else if (valueType == OperationType.UPDATE) {
            //是修改  就有两个公共字段数据
            try {
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                setUpdateTime.invoke(entity, now);

                setUpdateUser.invoke(entity, currentId);

            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }


    }
}
